using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Xml;

using ActiveDirectoryServices;

using Peradigm.Framework.Domain.Security;
using Peradigm.Framework.Security;

using NUnit.Framework;

namespace Liberty.SecurityProviders
{
    [TestFixture()]
    public class LDAPProviderTests
    {
        #region Constants
        #endregion

        #region Setup and Teardown
        [SetUp()]
        public void SetupLDAPProviderTests()
        {
            this.LDAPProvider = new LDAPProvider();
        }

        [TearDown()]
        public void TeardownLDAPProviderTests()
        {
            this.LDAPProvider = null;
        }
        #endregion

        #region Test Methods
        [Test()]
        public void TestHasUserWith()
        {
            Assert.AreEqual( false, this.LDAPProvider.HasUserWith( String.Empty, String.Empty ) );
        }

        [Test()]
        [ExpectedException( typeof( NotSupportedException ) )]
        public void TestLegacyUserWith()
        {
            this.LDAPProvider.LegacyUserWith( String.Empty, String.Empty, String.Empty );
        }

        [Test()]
        [ExpectedException( typeof( NotImplementedException ) )]
        public void TestLogout()
        {
            User testUser = new User();
            this.LDAPProvider.Logout( testUser );
        }

        [Test()]
        [Ignore( "Temporarily disabled." )]
        public void TestRolesForApplicationWithUserName()
        {
            Application application = new Application( "Liberty", "GUID", "Liberty" );
            string rolesXml = this.LDAPProvider.RolesFor( application, "Liberty.CaseDirector", "CaseDirector" );
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml( rolesXml );
            Console.WriteLine( xmlDoc.DocumentElement.OuterXml );
        }

        [Test()]
        [ExpectedException( typeof( NotSupportedException ) )]
        public void TestRolesForApplication()
        {
            this.LDAPProvider.RolesFor( null );
        }

        [Test()]
        [ExpectedException( typeof( Exception ) )]
        public void TestUsersIn()
        {
            this.LDAPProvider.UsersIn( null, null, null );
        }

        [Test()]
        [Ignore( "Temporarily disabled." )]
        public void TestActiveDirectoryUserWith()
        {
            this.LDAPProvider.ActiveDirectoryUserWith( "Liberty.CaseDirector", "CaseDirector" );
        }

        [Test()]
        [Ignore( "Temporarily disabled." )]
        public void TestGroupsForUser()
        {
            ActiveDirectoryUser user = this.LDAPProvider.ActiveDirectoryUserWith( "Liberty.CaseDirector@hdc.net", "CaseDirector" );
            List<Group> groups = this.LDAPProvider.GroupsForUser( user );
            foreach( Group group in groups )
            {
                Console.WriteLine( group.GroupName );
            }
        }

        [Test()]
        public void TestParseGroupNameFrom()
        {
            string distinguishedName = "CN=Liberty.CaseDirectors,OU=Groups,OU=Liberty,OU=Applications,OU=Peradigm,OU=Accounts,DC=hdc,DC=net";
            string groupName = this.LDAPProvider.ParseGroupNameFrom( distinguishedName );
            Assert.AreEqual( "Liberty.CaseDirectors", groupName, "Invalid Groupname with CN" );

            string noCNGroupName = "OU=Groups,OU=Liberty,OU=Applications,OU=Peradigm,OU=Accounts,DC=hdc,DC=net";
            groupName = this.LDAPProvider.ParseGroupNameFrom( noCNGroupName );
            Assert.AreEqual( String.Empty, groupName, "Invalid Groupname without CN" );

            string someGroupName = "CN=abcd,OU=Groups,OU=Liberty,OU=Applications,OU=Peradigm,OU=Accounts,DC=hdc,DC=net";
            groupName = this.LDAPProvider.ParseGroupNameFrom( someGroupName );
            Assert.AreEqual( "abcd", groupName, "Some Groupname with CN" );
        }

        #endregion

        #region Support Methods
        #endregion

        #region Properties
        private LDAPProvider LDAPProvider
        {
            get
            {
                return i_LDAPProvider;
            }
            set
            {
                i_LDAPProvider = value;
            }
        }
        #endregion

        #region Data Elements
        private LDAPProvider i_LDAPProvider;
        #endregion
    }
}
